package org.hhgzs.modules.erp.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hhgzs.modules.erp.entity.Material;
import org.hhgzs.modules.erp.entity.MaterialExtend;
import org.hhgzs.modules.erp.service.MaterialExtendService;
import org.hhgzs.modules.erp.service.MaterialService;
import org.hhgzs.modules.erp.vo.MaterialExtendVo4List;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品价格扩展 前端控制器
 * </p>
 *
 * @author peiqy
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/materialExtend")
@Slf4j
@RequiredArgsConstructor
@Api("结算账户管理")
public class MaterialExtendController extends JeecgController<MaterialExtend, MaterialExtendService> {

    private final MaterialExtendService materialExtendService;


    @GetMapping(value = "/getDetailList")
    public Result getDetailList(@RequestParam("materialId") Long materialId,
                                          HttpServletRequest request)throws Exception {
        try {
            List<MaterialExtendVo4List> dataList = new ArrayList<>();
            if(materialId!=0) {
                dataList = materialExtendService.getDetailList(materialId);
            }
            JSONObject outer = new JSONObject();
            outer.put("total", dataList.size());
            //存放数据json数组
            JSONArray dataArray = new JSONArray();
            if (null != dataList) {
                for (MaterialExtendVo4List md : dataList) {
                    JSONObject item = new JSONObject();
                    item.put("id", md.getId());
                    item.put("barCode", md.getBarCode());
                    item.put("commodityUnit", md.getCommodityUnit());
                    item.put("purchaseDecimal", md.getPurchaseDecimal());
                    item.put("commodityDecimal", md.getCommodityDecimal());
                    item.put("wholesaleDecimal", md.getWholesaleDecimal());
                    item.put("lowDecimal", md.getLowDecimal());
                    dataArray.add(item);
                }
            }
            outer.put("rows", dataArray);
            return Result.OK(outer);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取数据失败");
        }
    }

    @GetMapping(value = "/checkIsBarCodeExist")
    public Result checkIsBarCodeExist(@RequestParam("id") Long id,
                                      @RequestParam("barCode") String barCode,
                                      HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        try {
            int exist = materialExtendService.checkIsBarCodeExist(id, barCode);
            if (exist > 0) {
                map.put("status", true);
            } else {
                map.put("status", false);
            }
            return Result.OK(map);

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取数据失败");
        }
    }
}
